let yamlFn = require("./yamlFn");
let lib = require("./lib");
let qa = yamlFn.read();
let 返回牙叔教程 = require("./返回牙叔教程");
async function handleMessage(data, bot) {
  if (data.group_id > 0) {
    console.log(data.group_name);
    let 限制群名列表 = ["牙叔教程", "私人订制3"];
    // if (data.group_name !== 限制群名)  {
    if (限制群名列表.indexOf(data.group_name) < 0) {
      console.log("不在限制群名列表: " + data.group_name);
      return true;
    } else {
      console.log("是指定群: " + data.group_name);
    }
    let keyword = addQa(data);
    if (keyword) {
      console.log("向群组发送消息: " + data.group_name + " : " + "添加成功, 关键词: " + keyword);
      bot.sendGroupMsg(data.group_id, "添加成功, 关键词: " + keyword);
      return true;
    }
    let raw_message = data.raw_message;
    console.log("raw_message = " + raw_message);
    if (raw_message.indexOf("牙叔教程") > -1) {
      if (raw_message.length >= 6) {
        let msg = await 返回牙叔教程(raw_message.replace("牙叔教程", ""));
        console.log("向群组发送消息: " + data.group_name + " : " + msg);
        bot.sendGroupMsg(data.group_id, msg);
        return true;
      }
    }
    let public = qa.public;
    if (public[raw_message]) {
      console.log("属于公共关键词");
      console.log("向群组发送消息: " + data.group_name + " : " + public[raw_message]);

      bot.sendGroupMsg(data.group_id, public[raw_message]);
      return true;
    } else {
      console.log("不属于公共关键词");
    }
    let group_name = data.group_name;
    console.log("group_name = " + group_name);
    console.log("raw_message = " + raw_message);
    if (qa[group_name]) {
      console.log("有群组: " + group_name);
      if (qa[group_name][raw_message]) {
        console.log("发现关键词 : " + raw_message);
        console.log("向群组发送消息: " + data.group_name + " : " + qa[group_name][raw_message]);

        bot.sendGroupMsg(data.group_id, qa[group_name][raw_message]);
        return true;
      } else {
        console.log("没有发现关键词 : " + raw_message);
      }
    } else {
      console.log("没有群组: " + group_name);
    }
  }
}
function addQa(data) {
  let raw_message = data.raw_message;
  let user_id = data.user_id;
  if (raw_message.startsWith("添加===")) {
    console.log("是 开头 添加===");
    let arr = raw_message.split("===");
    if (arr.length < 2) {
      console.log("不符合添加qa格式");
      return false;
    } else {
      console.log("符合添加qa格式");
    }
    let keyword = arr[1];
    let answer = arr[2];
    if (!keyword || !answer) {
      console.log("没有keyword 或者 answer");
      return false;
    }
    console.log(`keyword = ${keyword}, answer = ${answer}`);
    console.log("qa =");
    console.log(qa);
    let group_name = data.group_name;
    console.log("group_name =");
    console.log(group_name);
    qa[group_name] = qa[group_name] || {};
    console.log(`qa[${group_name}] =`);
    console.log(qa[group_name]);
    qa[group_name][keyword] = answer + "\n   ---来自" + lib.desensitization(user_id + "", 2, 2);
    console.log(`qa[${group_name}][${keyword}] = `);
    console.log(qa[group_name][keyword]);
    yamlFn.write(qa);
    return keyword;
  } else {
    console.log("不是 开头 添加===");
  }
}
module.exports = handleMessage;

// if (data.group_id > 0) {
//   let raw_message = data.raw_message;
//   if (!!~raw_message.indexOf("t")) {
//     console.log('有t')
//     let content = qa.public.失明
//     console.log(content);
//     bot.sendGroupMsg(data.group_id, content);
//   }else{
//     console.log('无t')
//   }
// }

// {
//   self_id: 2191180850,
//   time: 1612439931,
//   post_type: 'message',
//   message_type: 'group',
//   sub_type: 'normal',
//   group_id: 1055918172,
//   group_name: '福宝复古单机交流群',
//   user_id: 1819250260,
//   anonymous: null,
//   message: [ { type: 'text', data: [Object] } ],
//   raw_message: '有打架的吗',
//   raw_message: '[CQ:face,id=263,text=/沧桑][CQ:face,id=263,text=/沧桑]',
//   font: '微软雅黑',
//   sender: {
//     user_id: 1819250260,
//     nickname: '俗',
//     card: '回收成品号',
//     sex: 'male',
//     age: 28,
//     area: '重庆',
//     level: 4,
//     role: 'member',
//     title: ''
//   },
//   message_id: 'PvAIXGxvjlQAACLgWVQrUGAb4XsB'
// }
